package ligne2vie.topos.client.maps.listeners;

import ligne2vie.topos.client.maps.MyMapWidget;
import ligne2vie.topos.shared.bo.CoordinatesConversion;
import ligne2vie.topos.shared.bo.TMapPoint;

import com.google.gwt.maps.client.base.InfoWindow;
import com.google.gwt.maps.client.event.EventCallback;
import com.google.gwt.maps.client.mvc.HasMVCObject;

/**
 * The class {@link OpenInfoWindowOnClick} allows displaying an information window about the map point which has just
 * been clicked on the point.
 * 
 * @author thuguerre
 * @since 1.0.0
 */
public class OpenInfoWindowOnClick extends EventCallback {

	private HasMVCObject mapElement;
	private TMapPoint mapPoint;

	/**
	 * Default constructor.
	 * 
	 * @param mapElement
	 *            The element which will be clicked.
	 * @param mapPoint
	 *            Corresponding map point.
	 */
	public OpenInfoWindowOnClick(HasMVCObject mapElement, TMapPoint mapPoint) {
		this.mapElement = mapElement;
		this.mapPoint = mapPoint;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void callback() {

		// --- constructing info window content
		
		String iwContent = "<b>" + mapPoint.getName() + "</b> (" + mapPoint.getAltitude() + " m&egrave;tres)";
		iwContent += "<br /><br />";
		iwContent += CoordinatesConversion.fromLatitude(mapPoint.getLatitude()).toString() + "<br />";
		iwContent += CoordinatesConversion.fromLongitude(mapPoint.getLongitude()).toString();

		if (mapPoint.getDescription() != null) {
			iwContent += "<br /><br />";
			iwContent += mapPoint.getDescription();
		}

		// --- creating info window object
		
		InfoWindow iw = new InfoWindow();
		iw.setContent(iwContent);
		
		// --- opening info window on the map
		
		MyMapWidget.getInstance().openInfoWindow(mapElement, iw);
	}
}
